import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:wowhy/models/admin_user_info.dart';

import 'action.dart';
import 'state.dart';

Reducer<AdminUserEditState> buildReducer() {
  return asReducer(
    <Object, Reducer<AdminUserEditState>>{
      AdminUserEditAction.load: _load,
      AdminUserEditAction.onEdit: _onEdit,
    },
  );
}

AdminUserEditState _load(AdminUserEditState state, Action action) {
  final AdminUserEditState newState = state.clone();
  final AdminUserInfo payload = action.payload ?? AdminUserInfo();

  newState.formKey = GlobalKey<FormBuilderState>();
  newState.rawModel = payload;
  newState.formModel = payload.clone();

  return newState;
}

AdminUserEditState _onEdit(AdminUserEditState state, Action action) {
  final AdminUserEditState newState = state.clone();

  newState.mode = 'edit';

  return newState;
}
